//
//  HZJ_SingerViewController.m
//  HZ_Beautiful_Music
//
//  Created by dllo on 16/4/6.
//  Copyright © 2016年 ui. All rights reserved.
//

#import "HZJ_SingerViewController.h"
#import "HZJSingerListViewController.h"
#import "HZJSingerCollecViewCell.h"
#import "HSAFNetTool.h"
#import "HZJSingerModel.h"
@interface HZJ_SingerViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) UICollectionView *collectionView;

@end

#define URL @"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=46&app=ttpod&v=v8.1.4.2016011318&user_id=0&mid=iPhone8,1&f=f320&s=s310&imsi=&hid=&splus=9.0.2&active=1&net=2&openudid=00e5496604ba470f229b34929ee14d0eb192a8e8&idfa=2301BEB2-C95C-4AA8-A905-794EC5CED975&utdid=VrPuK0MAZLUDAGfmA9wxwiTF&alf=201200&bundle_id=com.ttpod.music&latitude=38.88255365460551&longtitude=121.5392887772607"

@implementation HZJ_SingerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"歌手名单";
    
    [self createCollectionView];
    [self getData];
    
}
#pragma mark - 创建collectionView
- (void)createCollectionView {
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake((self.view.bounds.size.width - 40) / 3, (self.view.bounds.size.width - 40) / 3 + 30);
    flow.minimumLineSpacing = 10;
    flow.minimumInteritemSpacing = 0;
    flow.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flow];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_collectionView];
    
    [_collectionView registerClass:[HZJSingerCollecViewCell class] forCellWithReuseIdentifier:@"HZJSingerCollecViewCell"];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HZJSingerCollecViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HZJSingerCollecViewCell" forIndexPath:indexPath];
    cell.singerModel = self.dataSource[indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HZJSingerModel *singerModel = self.dataSource[indexPath.item];
    HZJSingerListViewController *listVC = [[HZJSingerListViewController alloc] init];
    
    listVC.singerModel = singerModel;
//    NSLog(@"%@", singerModel.idNum);
    [self.navigationController pushViewController:listVC animated:YES];
    
    [_collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
}

#pragma mark - 数据请求
- (void)getData {
    
    [HSAFNetTool GETNetWithUrl:URL body:nil headerFile:nil response:HSJSON success:^(id result) {

        self.dataSource = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in result[@"data"]) {
            HZJSingerModel *singerModel = [[HZJSingerModel alloc] initWithDataSource:dic];
            [self.dataSource addObject:singerModel];
        }
        [_collectionView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
